package com.cheer.ba.view.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.model.AskGetMoneyModel;
import com.cheer.ba.model.NextLevleModel;
import com.cheer.ba.model.ThreeLevleModel;
import com.cheer.ba.presenter.impl.AskQuestPresenter;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.view.adapter.ExpandableItemAdapter;
import com.cheer.ba.view.base.BaseCommAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: hq
 * Email: 834138841@qq.com
 * Comment: //TODO 查看我邀请的好友列表
 * Date: 2017-07-28 13:39
 */
public class MyRequestAllFriendAty extends BaseCommAty {

    @BindView(R.id.id_ask_friends_recycle)
    RecyclerView mRecycleView;
    //adapter
    private ExpandableItemAdapter adapter;
    //存储好友列表
    private ArrayList<MultiItemEntity> mSaveFriends = new ArrayList<MultiItemEntity>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_my_friends, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new AskQuestPresenter();
    }

    @Override
    public void initView() {
        setTitleText("邀请好友列表");

    }

    @Override
    public void initData() {
        ((AskQuestPresenter) presenter).getFriendList(true);
    }

    /**
     * 处理列表数据
     *
     * @param mFriends
     */
    private void handList(List<AskGetMoneyModel> mFriends) {
        mSaveFriends.clear();
        for (AskGetMoneyModel item1 : mFriends) {
            List<NextLevleModel> mLevle2s = item1.getNextLevel();
            if (!mLevle2s.isEmpty()) {
                for (NextLevleModel item2 : mLevle2s) {
                    List<ThreeLevleModel> mLevle3s = item2.getNextLevel();
                    if (!mLevle3s.isEmpty()) {
                        for (ThreeLevleModel item3 : mLevle3s) {
                            item2.addSubItem(item3);
                        }
                    }
                    item1.addSubItem(item2);
                }
            }
            mSaveFriends.add(item1);
        }
        adapter = new ExpandableItemAdapter(mSaveFriends);
        final GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return adapter.getItemViewType(position) == ExpandableItemAdapter.TYPE_PERSON ? 1 : manager.getSpanCount();
            }
        });
        mRecycleView.setAdapter(adapter);
        mRecycleView.setLayoutManager(manager);
        adapter.expandAll();
//        adapter.notifyDataSetChanged();
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_MY_FRIENDS)) {
                LogUtils.LogD("哈哈", "进来了");
                //获得我的好友列表
                final List<AskGetMoneyModel> mFriends = (List<AskGetMoneyModel>) t;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        handList(mFriends);
                    }
                });
            }
        }
    }
}
